#include<bits/stdc++.h>
using namespace std;
int main() {
	int t;
	cin >> t;
	while (t--) {
		int n, m;
		cin >> n >> m;
		int mx = m;
		for (int i = 1; i <= 1000; i++) {
			m *= m;
			m = stoi(to_string(m).substr(0, n));
			mx = max(mx, m);
		}
		cout << mx << endl;
	}
	return 0;
}

